package ganga.custodian;

import java.util.List;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.location.LocationManager;
import android.os.AsyncTask;

//import ganga.custodian.human.HumanModel;
import ganga.custodian.notification.NotificationExecutor;
import ganga.custodian.human.EmergencyDetector;
import ganga.custodian.human.HumanModel;
import ganga.custodian.human.PermissibleRange;

/**
 * @author Apocrypha
 * Main singlton
 * @see http://www.androidcookbook.com/Recipe.seam?recipeId=1218
 */
public final class CustodianApplication extends Application {

	private Context context = this;
	//private SensorManager mSensorManager;
	//private final Sensor mAccelerometer;
	public HumanModel humanModel;
	public NotificationExecutor notificationExecutor;
	public LocationManager locationManager;
	public SensorManager sensorManager;
	public EmergencyDetector emergencyDetector;
	


	private void initialize(){
		new DataServiceTask().execute();
		locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		emergencyDetector = new EmergencyDetector();
	}
	private void initializeSensorManager(){
		sensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
		@SuppressWarnings("unused")
		List<Sensor> sl;
		sl = sensorManager.getSensorList(Sensor.TYPE_ALL);
		//mAccelerometer = mSensorManager;
	}
	//Implementations should be as quick as possible 
	//(for example using lazy initialization of state) 
	//since the time spent in this function directly impacts 
	//the performance of starting the first activity, service, or receiver in a process. 
	@Override
	public final void onCreate() {
		super.onCreate();
		instance = this;
		instance.initialize();
		instance.initializeSensorManager();
		humanModel = new HumanModel();
		notificationExecutor = new NotificationExecutor(this);
	}
	//Can be called when the application object is terminated. 
	//Note that there is no guarantee of this method handler�s being called. 
	//If the application is terminated by the kernel 
	//in order to free resources for other applications, 
	//the process will be terminated without warning and 
	//without a call to the application object�s onTerminate handler.
	@Override
	public final void onTerminate() {
		//TODO specify name for stopService(name) 
		stopService(null);
		super.onTerminate();
	}

	/**
	 * Singleton
	 */
	private static CustodianApplication instance;
	/**
	 * Returns the application instance
	 */
	public static CustodianApplication getInstance() {
		return instance;
	}
	private class DataServiceTask extends AsyncTask <Void,Void,Void>{

		@Override
		protected Void doInBackground(Void... arg0) {
			Intent startServiceIntent = new Intent(context, HeartBeatService.class);
			startService(startServiceIntent);
			return null;
		}		
	} //DataServiceTask
}//CustodianApplication